package com.husd.framework.zookeeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * @author hushengdong
 */
public class WatcherTest {

    static final String path = "/watcher";
    private static String serverList = "127.0.0.1:2181";


    public static void main(String[] args) throws Exception {

        MyZookeeperWatcher myZookeeperWatcher = new MyZookeeperWatcher();
        ZooKeeper zooKeeper = new ZooKeeper(serverList, 4000, myZookeeperWatcher);
        myZookeeperWatcher.setZooKeeper(zooKeeper);
        if (zooKeeper.exists(path, false) == null) {
            zooKeeper.create(path, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        Thread.sleep(1000);
        System.out.println("-----------");
        //true表示使用zookeeper实例中配置的watcher
        Stat stat = zooKeeper.exists(path, true);
        System.out.println(stat.getMtime());
        System.in.read();
    }
}
